<?php include dirname(__FILE__).'/common/header.php'; ?>

<?php include dirname(__FILE__).'/common/menu.php'; ?>
	var attr = {};
	attr.project_list = <?php echo $attr['project_list'] ?>;
	attr.project_id = Ext.util.Cookies.get('project_id')||'';

	list.grid = new ZM.GridPanel({
		url: list.baseUrl + 'widget/getWidgetList',
		history:'widget_id',
		onlyShow: true,
		region: 'center',
		tbar:[
			{
				text:'创建模块',
				cls:'btn-show',
				iconCls:'icon-create',
				handler:function(){
					var frm = new ZM.FormPanel({
						url : list.baseUrl + 'widget/createWidget',
						keySubmit:true,
						success:function(res) {
							ZM.bo.msg('success',res.detail);
							attr.project_id = res.addon.project_id;
							Ext.util.Cookies.set('project_id',res.addon.project_id);
							list.grid.store.reload();
							win.close();
						},
						items:[
							{
								fieldLabel:'模块名称',
								name:'widget_name',
								allowBlank:false
							},
							{	
								xtype:'combofield',
								fieldLabel:'项目名称',
								name:'project_id',
								value:attr.project_id,
								options:attr.project_list,
								allowBlank:false
							}
						]
					});
					var win = new ZM.PopUp({
						title:'创建模块',
						type:'s',
						items:frm
					});
					win.show();
				}
			}
		],
		//group:{field:'project_id',groupView:true,tpl:'{group}'},
		columns:[
			{header:'ID',dataIndex:'widget_id',filter:{},width:80,sort:true},
			{header:'模块名字',dataIndex:'widget_name',width:140,filter:{}},
			{header:'项目名称',dataIndex:'project_id',width:110
				,renderer:function(val) {
					return ZM.id2name(val,attr.project_list);
				},filter:{
				xtype:"combofield",
				listWidth:180,
				options:[['','全部']].concat(attr.project_list)
			}},
			{header:'应用模版数',dataIndex:'template_count',width:80
				,renderer:function(val) {
					if(val == 0) {
						return '<span style="color:red;">'+val+'</span>';
					}else{
						return '<span>'+val+'</span>';
					}
				}
			},
			{header:'最后更改用户',dataIndex:'user_name',width:90},
			{header:'最后修改时间',dataIndex:'widget_time_lastmodified',renderer:ZM.diffDay,width:140,sort:true},
			{header:'操作',dataIndex:'',renderer:function(val,m,r) {
					var str = '';
					//str += '<a href="/widget_edit/'+r.json.widget_id+'" target="_blank">[编辑内容]</a>';
					str += Ext.rd('编辑内容','fireDetail',r,'widget/index');
					str += Ext.rd('编辑信息','fireEditWidget',r,'widget/editWidgetBaseInfo');
					//str += Ext.rd('应用模板列表','fireTemplate',r,'widget/getWidgetTemplateList');
					str += Ext.rd('复制','fireCopyWidget',r,'widget/copyWidget');
					if(r.json.template_count == 0) {
						str += Ext.rd('删除','fireDeleteWidget',r,'widget/deleteWidget');
					}
					
					return str;
				}
			}
		]
	});

	
	list.grid.fireEditWidget = function(r) {
		var r = r.json;
		var frm = new ZM.FormPanel({
			url : list.baseUrl + 'widget/editWidgetBaseInfo',
			keySubmit:true,
			params:{widget_id:r.widget_id},
			success:function(res) {
				ZM.bo.msg('success',res.detail);
				list.grid.store.reload();
				win.close();
			},
			items:[
				{
					fieldLabel:'模块名称',
					name:'widget_name',
					value:r.widget_name,
					allowBlank:false
				},
				{	
					xtype:'combofield',
					fieldLabel:'项目名称',
					name:'project_id',
					value:r.project_id,
					options:attr.project_list,
					allowBlank:false
				}
			]
		});
		var win = new ZM.PopUp({
			title:'编辑模块['+r.widget_name+']',
			type:'s',
			items:frm
		});
		win.show();
	}

	list.grid.fireCopyWidget = function(r) {
		var r = r.json;
		var frm = new ZM.FormPanel({
			url : list.baseUrl + 'widget/copyWidget',
			keySubmit:true,
			params:{widget_id:r.widget_id},
			success:function(res) {
				ZM.bo.msg('success',res.detail);
				list.grid.store.reload();
				win.close();
			},
			items:[
				{
					fieldLabel:'模块名称',
					name:'widget_name',
					value:r.widget_name,
					allowBlank:false
				},
				{	
					xtype:'combofield',
					fieldLabel:'项目名称',
					name:'project_id',
					value:r.project_id,
					options:attr.project_list,
					allowBlank:false
				}
			]
		});
		var win = new ZM.PopUp({
			title:'复制模块['+r.widget_name+']',
			type:'s',
			items:frm
		});
		win.show();
	}
	
	list.grid.fireDeleteWidget = function(r) {
		var r = r.json;
		Ext.Msg.confirm('确认?','确定要删除模块['+r.widget_name+']?',function(btn){
			if(btn == 'yes') {
				list.grid.getEl().mask('正在删除中...','x-mask-loading');
				ZM.ajax({
					url: list.baseUrl + 'widget/deleteWidget',
					params: {widget_id:r.widget_id},
					success:function(res){
						list.grid.getEl().unmask();
						ZM.bo.msg('success',res.detail);
						list.grid.store.reload();
					},
					failure:function(res) {
						list.grid.getEl().unmask();
					}
				});
			}
		});
	}


	
	list.grid.fireTemplate = function(r) {
		var r = r.json;
		list.tId = r.widget_id;
		list.detail.show().ownerCt.doLayout();
		templateGrid.store.baseParams = {widget_name:r.widget_name,project_id:r.project_id};
		templateGrid.store.load();
	}

	list.grid.fireDetail = function(r) {
		var r = r.json;
		//location.href="/widget_edit/"+r.widget_id;
		window.open("/widget_edit/"+r.widget_id);
	}
	
	var templateGrid = new ZM.GridPanel({
		url: list.baseUrl + 'widget/getWidgetTemplateList',
		history:'template_id',
		onlyShow: true,
		columns:[
			{header:'ID',dataIndex:'template_id',width:80},
			{header:'模板名称',dataIndex:'template_name',width:140},
			{header:'项目名称',dataIndex:'project_id',width:120
				,renderer:function(val) {
					return ZM.id2name(val,attr.project_list);
				}
			},
			{header:'最后更改用户',dataIndex:'user_name',width:80},
			{header:'最后修改时间',dataIndex:'template_time_lastmodified',width:140},
			{header:'操作',dataIndex:'',renderer:function(val,m,r) {
					var str = '';
					str += '<a href="#" id="_'+r.id+'" title="fireEditTemplate">[编辑]</a>';
					str += '<a href="/t/'+r.json.template_id+'" target="_blank">[预览]</a>';

					return str;
				}
			}
		]
	});

	templateGrid.fireEditTemplate = function(r) {
		var r = r.json;
		var frm = new ZM.FormPanel({
			tbar:[
				{
					text:'保存',
					iconCls:'icon-save',
					hidden:!Ext.permission['template/editTemplate'.toLowerCase()],
					cls:'btn-show',
					handler:function(){
						ZM.submitForm(frm);
					}
				}
			],
			region:'center',
			url:list.baseUrl + 'template/editTemplate',
			keySubmit:false,
			focusField: false,
			buttons:null,
			params:{template_id:r.template_id},
			success:function(res){
				ZM.bo.msg('操作成功',res.detail);
				list.grid.store.reload();
				frm.params.template_time_lastmodified = res.addon.template_time_lastmodified;
			},
			defaults:{width:780},	
			items:[{
				fieldLabel:'CSS',
				name:'template_css',
				height: 150,
				xtype:'textarea',
				id : 'csscreate',
				plugins:[new ZM.tabKey()]
			},{
				fieldLabel:'HTML',
				name:'template_struct',
				id:'field_template_struct',
				height: 250,
				xtype:'textarea',
				plugins:[new ZM.tabKey()],
				allowBlank:false,
				enableKeyEvents: true,
				listeners:{
					'afterrender':function(f) {
						f.el.on('mousedown',function(e,t){
							if(strWidget!='') {
								var me = this;
								setTimeout(function() {
										ZM.insertAtCursor(me.dom, strWidget);
										strWidget = '';
									},50);
							}
						},f.el);
					}
				}
			}]
		});		
		var strWidget = '';
		var wGrid = new ZM.GridPanel({
			url: list.baseUrl + 'template/getProjectWidgetList',
			params:{project_id:r.project_id},
			onlyShow:true,
			region:'east',
			width:130,
			collapsible:true,
			titleCollapse: true,
			bbar:null,
			columns:[
				{header:'ID',dataIndex:'widget_id',hidden:true},
				{header:'模块名称',dataIndex:'widget_name',width:120,filter:{},renderer:function(val,m,r) {
					var str = '';
					str += '<a href="#" title="fireInsertWidget" id="_'+r.id+'" style="color:#111;text-decoration:none;display:block;width:100%;">'+val+'</a>';
					return str;
				}}
			],
			fireInsertWidget:function(r) {
				ZM.insertAtCursor(Ext.get('field_template_struct').dom,'<widget>'+r.json.widget_name+'</widget>\n');
			}
		});
		var win = new ZM.PopUp({
			title:' 编辑模板['+r.template_name+']代码',
			type:'l',
			width:1030,
			layout:'border',
			items: [frm,wGrid]
		});
		win.show();
		wGrid.store.load();
		frm.getEl().mask('数据获取中...','x-mask-loading');
		ZM.ajax({
			url:list.baseUrl + 'template/getTemplate',
			params:{template_id:r.template_id},
			success:function(res) {
				frm.getForm().findField('template_css').setValue(res.addon.template.template_css);
				frm.getForm().findField('template_struct').setValue(res.addon.template.template_struct);
				frm.params.template_time_lastmodified = res.addon.template.template_time_lastmodified;
				frm.getEl().unmask();
			},
			failure:function(res) {
				Ext.Msg.alert('失败','获取数据失败!点击确定关闭窗口!',function(){
					win.close();
				});
			}
		});
	}
	
	list.detail = new ZM.detailPanel({
		items: templateGrid
	});
	 
<?php include dirname(__FILE__).'/common/layout.php'; ?>
list.grid.store.load();
<?php include dirname(__FILE__).'/common/footer.php'; ?>